﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Freebase4net.Tests
{
    [TestClass]
    public class MqlReadServicePerfomanceTests
    {
        private MqlReadService _readService;

        public MqlReadServicePerfomanceTests()
        {
            _readService = FreebaseServicesBuilder.CreateMqlReadService();
        }

        [TestMethod]
        public void ReadAsync_MultipleQueries_Perfomance()
        {
            long first, second;
            List<dynamic> queries = new List<dynamic>(40);
            for (int i = 0; i < 40; i++)
            {
                dynamic thescorpions = new ExpandoObject();
                thescorpions.name = "The Scorpions";
                thescorpions.type = "/music/artist";
                queries.Add(thescorpions);
            }

            //first test
            Stopwatch watch = new Stopwatch();
            watch.Start();
            MultipleMqlReadServiceAsyncResponse multiFreebaseResponse = _readService.ReadMultipleAsync(queries.ToArray());
            Task.WaitAll(multiFreebaseResponse.Results.ToArray());
            watch.Stop();
            first = watch.ElapsedMilliseconds;

            //second test
            watch = new Stopwatch();
            watch.Start();
            MultipleMqlReadServiceResponse multiResponse = _readService.ReadMultiple(queries.ToArray());
            watch.Stop();
            second = watch.ElapsedMilliseconds;

            Assert.AreEqual(true, first < second);
        }

        [TestMethod]
        public void SpeedTest_AsyncVsSync()
        {
            long first, second;
            Stopwatch watch = new Stopwatch();

            dynamic director = new ExpandoObject();
            director.type = Freeb.Operators.CreateOneOfOperator(new object[] { "/music/recording", "/film/film", "/boo/book" });
            director.name = Freeb.Operators.CreateLikeOperator("^the scor*$");
            director.id = null;

            //First Test
            watch.Start();
            List<Task<MqlReadServiceResponse>> list = new List<Task<MqlReadServiceResponse>>(40);
            for (int i = 0; i < 40; i++)
            {
                list.Add(_readService.ReadAsync(director));
            }

            Task.WaitAll(list.ToArray());
            watch.Stop();
            first = watch.ElapsedMilliseconds;

            //Second Test
            Thread.Sleep(2000);
            watch = new Stopwatch();
            watch.Start();
            for (int i = 0; i < 40; i++)
            {
                var res2 = _readService.Read(director);
            }

            watch.Stop();
            second = watch.ElapsedMilliseconds;

            Assert.AreEqual(true, first < second);

        }
    }
}
